package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.dto.font.CourseQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.Course;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.CourseService;
import com.jeff.tianti.cms.service.TeachService;
import com.jeff.tianti.common.PageBean;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
@CrossOrigin(maxAge = 3600)
public class CourseController {
    @Autowired
    CourseService courseService;

    @Autowired
    ColumnInfoService columnInfoService;

    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult getCourseList(@RequestBody PageBean pageBean) throws RuntimeException {
        PageModel<Map<String, Object>> coursePage = courseService.queryCourseList(pageBean);
        return AjaxResult.setSuccessPageModelDate(coursePage);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult getCourseDetail(@RequestBody CourseQueryDTO courseQueryDTO) throws RuntimeException {
        Course course = courseService.queryCourseById(courseQueryDTO.getId());
        return AjaxResult.setSuccessData(course);
    }

    /**
     * 是否精英课程
     *
     * @return
     * @throws RuntimeException
     */
    @RequestMapping(value = "/cream/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult getCreamList() throws RuntimeException {
        PageModel<Map<String, Object>> coursePageModel = courseService.queryCourseListByBast(1, 6);
        return AjaxResult.setSuccessPageModelDate(coursePageModel);
    }

    /**
     * 按开班信息 列举最近6天的 课程时间
     *
     * @return
     * @throws RuntimeException
     */
    @RequestMapping(value = "/lastSixDay", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult listLastSixDay() throws RuntimeException {
        List<Map> lastSixDay = courseService.getLastSixDay();
        return AjaxResult.setSuccessData(lastSixDay);
    }

    /**
     * 根据时间查询 课程
     *
     * @return
     * @throws RuntimeException
     */
    @RequestMapping(value = "/listByDay", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult listLastSixDayCourse(@RequestBody CourseQueryDTO courseQueryDTO) throws RuntimeException {
        List lastSixDayCourse = courseService.getLastSixDayCourse(courseQueryDTO.getDay());
        return AjaxResult.setSuccessData(lastSixDayCourse);
    }

    /**
     * 根据分类查询课程列表
     *
     * @return
     * @throws RuntimeException
     */
    @RequestMapping(value = "/listCourseByCate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult getCourseByColumnId(@RequestBody CourseQueryDTO courseQueryDTO) throws RuntimeException {
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(courseQueryDTO.getColumnId());
        columnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> columnInfos = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        List result=new ArrayList();
        for (int i = 0; i < columnInfos.size(); i++) {
            ColumnInfo columnInfo = columnInfos.get(i);
            String name = columnInfo.getName();
            List<Map<String, Object>> courseList = courseService.getCourseListByCateId(columnInfo.getId());
            Map cateCourseMap = new HashMap<>();
            cateCourseMap.put("cateName",name);
            cateCourseMap.put("courseList",courseList);
            if (!CollectionUtils.isEmpty(courseList)){
                result.add(cateCourseMap);
            }
        }
        return AjaxResult.setSuccessData(result);
    }


}
